# Cross-compilation support
# Use environment variables: CC, CXX, AR, STRIP
# If not set, default to native tools
CC ?= gcc
CXX ?= g++
AR ?= ar
STRIP ?= strip

# Default compiler flags
CFLAGS ?= -std=c11 -Wall -Wextra -O2
CXXFLAGS ?= -std=c++11 -Wall -Wextra -O2
LDFLAGS ?=

# Find all subdirectories containing Makefile
SUBDIRS := $(shell find . -maxdepth 1 -type d -name "[^.]*" | while read dir; do if [ -f "$$dir/Makefile" ]; then echo "$$dir" | sed 's|^\./||'; fi; done)

# Export variables to sub-makes
export CC CXX AR STRIP CFLAGS CXXFLAGS LDFLAGS

.PHONY: all clean $(SUBDIRS)

# Default target - build all projects
all: $(SUBDIRS)

# Target for each subdirectory - compile and strip
$(SUBDIRS):
	@$(MAKE) -C $@
	@$(MAKE) -C $@ strip

# Clean targets
clean:
	$(foreach dir,$(SUBDIRS),$(MAKE) -C $(dir) clean;)
